import pandas as pd

# df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 8, 7]], columns=['one', 'two', 'three'])
df = pd.DataFrame([[1, 2, 3], [1, 8, 7]], columns=['one', 'two', 'three'])

print(df)
print("----------")


def calculate_p(params):
    """
    计算P10， P90
    """
    # 只给p10, p90赋值一次
    p10, p90 = -1, -1
    if len(params) == 1:
        # 若只有一种数值，则P10=0, P90=该数值
        p10, p90 = 0, params.index[0]
        return p10, p90
    temp_p = 0  # 临时参数，用来计算累计概率
    for row in params.index:
        temp_p = temp_p + params[row]
        if p10 != -1 and p90 != -1:
            break
        if p10 == -1:
            # 当概率为10%的时候，将该值赋给p10
            if temp_p >= 0.1:
                p10 = row
        if p90 == -1:
            if temp_p >= 0.9:
                p90 = row
    return p10, p90


p10_list, p90_list = [], []
for column in df.columns:
    ser = df[column]
    cou = ser.value_counts()
    cou_sort = cou.sort_index(axis=0, ascending=True)
    cou_p = cou_sort / len(df[column])
    print("---------%s---------" % column)
    print(cou_p)
    p10, p90 = calculate_p(cou_p)
    p10_list.append(p10)
    p90_list.append(p90)
print(p10_list, p90_list)
